package com.abc.robot01.activity.compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.animateIntOffsetAsState
import androidx.compose.animation.core.animateSizeAsState
import androidx.compose.animation.core.repeatable
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideIn
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOut
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import kotlin.math.roundToInt

//https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/PullToRefreshSamples.kt

class AnimationsSamples : ComponentActivity() {
    // Demo items for Flow row / flow column
    @OptIn(ExperimentalFoundationApi::class)
    @Composable
  fun   HorizontalPager01()
    {
        // Display 10 items
        val pagerState = rememberPagerState(pageCount = {
            10
        })
        HorizontalPager(state = pagerState) { page ->
            // Our page content
            Text(
                text = "Page: $page",
                modifier = Modifier.fillMaxWidth()
            )
        }
    }

//    @Composable
//    fun Animations01(){
//        var visible =   remember {
//            mutableStateOf(true)
//        }
////// Animated visibility will eventually remove the item from the composition once the animation has finished.
//        AnimatedVisibility(visible) {
//            // your composable here
//            // ...
//            HorizontalPager01()
//        }
//
//
//    }

    @Composable
    fun AnimatedVisibility01() {
        var visible by remember { mutableStateOf(true) }

        Column(horizontalAlignment = Alignment.Start) {
            Button(onClick = { visible = !visible }) {
                Text(text = if (visible) "Hide" else "Show")
            }

            Spacer(modifier = Modifier.height(20.dp))

            AnimatedVisibility(visible = visible,
                enter = fadeIn()+ expandVertically(),
                exit = fadeOut() + shrinkVertically()

            ) {
                Box(
                    modifier = Modifier
                        .size(1000.dp)
                        .background(Color.Blue),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "Hello", color = Color.White)
                }
            }
        }
    }

    @Composable
    fun AnimatedContentSizeDemo() {
        var expanded by remember { mutableStateOf(false) }

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
                .animateContentSize(
                    animationSpec = tween(
                        durationMillis = 5000,
                        easing = LinearOutSlowInEasing
                    )
                ), // 添加动画效果
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = if (expanded) "This is a longer text that expands the view." else "Short text.",
                modifier = Modifier.padding(8.dp)
            )

            Spacer(modifier = Modifier.height(16.dp))

            Button(onClick = { expanded = !expanded }) {
                Text(text = if (expanded) "Collapse" else "Expand")
            }
        }
    }

    @Composable
    fun SimpleAnimatedContentSizeDemo() {
        var expanded by remember { mutableStateOf(false) }

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
                .animateContentSize(
                    animationSpec = tween(
                        durationMillis = 5000,
//                    easing = LinearOutSlowInEasing
                    )
                ), // 添加动画效果
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(onClick = { expanded = !expanded }) {
                Text(text = if (expanded) "Collapse" else "Expand")
            }

            if (expanded) {
                Box(modifier = Modifier
                    .height(800.dp)
                    .fillMaxWidth()
                    .background(Color.Blue)
                    .animateContentSize())  {
                Text(
                    text = "This is a longer text that expands the view.",
                    modifier = Modifier.padding(8.dp)
                )
                }

            }
        }
    }

    @Composable
    fun Offset01(){
        var moved by remember { mutableStateOf(false) }
        val pxToMove = with(LocalDensity.current) {
            100.dp.toPx().roundToInt()
        }
        val offset by animateIntOffsetAsState(animationSpec = tween(
            durationMillis = 1000),
            targetValue = if (moved) {
                IntOffset(pxToMove, pxToMove)
            } else {
                IntOffset.Zero
            },
            label = "offset"
        )

        Box(
            modifier = Modifier
                .offset {
                    offset
                }
                .background(Color.Blue)
                .size(100.dp)
                .clickable(
                    interactionSource = remember { MutableInteractionSource() },
                    indication = null
                ) {
                    moved = !moved
                }
        )
    }


    @OptIn(ExperimentalLayoutApi::class)
    @Composable
    private fun FlowRowSimpleUsageExample() {
        FlowRow(modifier = Modifier.padding(8.dp)) {
            Text("Price: High to Low")
            Text("Price: High to Low")
            Text("Price: High to Low")
            Text("Price: High to Low")
            Text("Price: High to Low")
            Text("Price: High to Low")
            Text("Price: High to Low")
            Text("Price: High to Low")
            Text("Price: High to Low")

        }
    }


    @Composable
    fun BoxWithBackgroundColor() {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxSize()
//                .size(200.dp)
                .background(Color.Blue) // 设置背景颜色为蓝色
        ) {
            Text(
                text = "Hello, World!",
                color = Color.White,
                modifier = Modifier.align(Alignment.Center)
            )
        }
    }

    @Composable
    fun BackgroundAlphaAnim01() {
        var isVisible by remember { mutableStateOf(true) }

        val alpha: Float by animateFloatAsState(targetValue = if (isVisible) 1f else 0f , animationSpec= tween(durationMillis=2000))

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
            ,horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(onClick = { isVisible = !isVisible }) {
                Text(text = if (isVisible) "Visible" else "InVisible")
            }

            Box(modifier = Modifier
                .height(200.dp)
                .fillMaxWidth()
                .background(Color.Red.copy(alpha = alpha))) {
                    Text(
                        text = "This is a longer text that expands the view.",
                        modifier = Modifier.padding(8.dp)
                    )
                }
        }
    }


    @Composable
    fun TextColorAnim01() {
        var isVisible by remember { mutableStateOf(true) }
        // Animate the text color between Red and Blue
        val textColor by animateColorAsState(targetValue = if (isVisible) Color.Red else Color.Blue,animationSpec= tween(durationMillis=5000))

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
            ,horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(onClick = { isVisible = !isVisible }) {
                Text(text = if (isVisible) "Red" else "Blue")
            }

            Box(modifier = Modifier
                .height(200.dp)
                .fillMaxWidth()
                .background(Color.White)) {
                Text(
                    color = textColor,
                    text = "This is a longer text that expands the view.",
                    modifier = Modifier
                        .padding(8.dp)
                        .height(100.dp)
                )
            }
        }
    }

    @Composable
    fun Scale() {
        var visible by remember { mutableStateOf(true) }

        Column(horizontalAlignment = Alignment.Start) {
            Button(onClick = { visible = !visible }) {
                Text(text = if (visible) "Hide" else "Show")
            }

            Spacer(modifier = Modifier.height(20.dp))

            AnimatedVisibility(visible = visible,
                enter = scaleIn(animationSpec= tween(2000)),
                exit = scaleOut(animationSpec = tween(2000))

            ) {
                Box(
                    modifier = Modifier
                        .size(1000.dp)
                        .background(Color.Blue),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "Hello", color = Color.White)
                }
            }
        }
    }

    @Composable
    fun Slide() {
        var visible by remember { mutableStateOf(true) }
        Column(horizontalAlignment = Alignment.Start) {
            AnimatedVisibility(visible = visible,
                enter = slideInVertically(animationSpec= tween(2000)),
                exit = slideOutVertically(animationSpec = tween(2000))
            ) {
                Box(
                    modifier = Modifier
                        .size(500.dp)
                        .background(Color.Blue),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "Hello", color = Color.White)
                }
            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Button(onClick = { visible = !visible }) {
            Text(text = if (visible) "Hide" else "Show")
        }
    }

    @Composable
    fun Spring() {
        var visible by remember { mutableStateOf(true) }
        Column(horizontalAlignment = Alignment.Start) {
            AnimatedVisibility(visible = visible,
                enter = scaleIn(animationSpec = spring()),
                exit = scaleOut(animationSpec = spring())
            ) {
                Box(
                    modifier = Modifier
                        .size(200.dp)
                        .background(Color.Blue),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "Hello", color = Color.White)
                }
            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Button(onClick = { visible = !visible }) {
            Text(text = if (visible) "Hide" else "Show")
        }
    }

    @Composable
    fun SlideInFromLeftDemo() {
        var visible by remember { mutableStateOf(true) }
        Column(horizontalAlignment = Alignment.Start) {
            AnimatedVisibility(visible = visible,
                enter = slideInHorizontally(initialOffsetX = { -it }),
                exit = slideOutHorizontally(targetOffsetX = { -it })
            ) {
                Box(
                    modifier = Modifier
                        .size(200.dp)
                        .background(Color.Blue),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "Hello", color = Color.White)
                }
            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Button(onClick = { visible = !visible }) {
            Text(text = if (visible) "Hide" else "Show")
        }
    }

    @Composable
    fun GraphicsLayer01() {
         var visible  by remember { mutableStateOf(true)
         }
        val textColor by animateFloatAsState(targetValue = if (visible) 1.0f else 0.0f,animationSpec = tween(durationMillis = 2000)
        )
        Column(horizontalAlignment = Alignment.Start) {
                Box(
                    modifier = Modifier
                        .size(200.dp)
                        .graphicsLayer {
                            alpha = textColor
                        }
                        .background(Color.Blue),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "Hello", color = Color.White)
                }
//            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Button(onClick = { visible = !visible }) {
            Text(text = if (visible) "Hide" else "Show")
        }
    }

    @Composable
    fun GraphicsLayer02() {
        var visible  by remember { mutableStateOf(true)
        }
        val textColor by animateFloatAsState(targetValue = if (visible) 1.0f else 0.0f,animationSpec = tween(durationMillis = 2000)
        )
        Column(horizontalAlignment = Alignment.Start) {
            Box(
                modifier = Modifier
                    .size(200.dp)
                    .graphicsLayer {
                        scaleX = textColor
                        scaleY = textColor

                    }
                    .background(Color.Blue),
                contentAlignment = Alignment.Center
            ) {
                Text(text = "Hello", color = Color.White)
            }
//            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Button(onClick = { visible = !visible }) {
            Text(text = if (visible) "Hide" else "Show")
        }
    }

    @Composable
    fun RepeatingRotationAnimation() {
        var visible  by remember { mutableStateOf(true)
        }
        val scale by animateFloatAsState(targetValue = if (visible) 0.0f else 360.0f,animationSpec =
        repeatable(iterations = 2,
            animation =  tween(durationMillis = 2000),
            repeatMode = RepeatMode.Reverse
            )
        )
        Column(horizontalAlignment = Alignment.Start) {
            Box(
                modifier = Modifier
                    .size(200.dp)
                    .graphicsLayer {
                        rotationZ =scale

                    }
                    .background(Color.Blue),
                contentAlignment = Alignment.Center
            ) {
                Text(text = "Hello", color = Color.White)
            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Button(onClick = { visible = !visible }) {
            Text(text = if (visible) "Hide" else "Show")
        }
    }

    @Composable
    fun AnimateSizeAsState01() {
        var visible  by remember { mutableStateOf(true)
        }
        val scale by animateSizeAsState(targetValue = if (visible)  Size(200.dp.value, 200.dp.value) else  Size(0.dp.value, 0.dp.value),animationSpec =
          tween(durationMillis = 2000)
        )
        Column(horizontalAlignment = Alignment.Start) {
            Button(onClick = { visible = !visible }) {
                Text(text = if (visible) "Hide" else "Show")
            }
            Spacer(modifier = Modifier.height(20.dp))
            Box(
                modifier = Modifier
                    .size(scale.width.dp,scale.height.dp)
                    .background(Color.Blue),
                contentAlignment = Alignment.Center
            ) {
                Text(text = "animateSizeAsState", color = Color.White)
            }
        }
    }

//    https://github.com/android/snippets/blob/1a6cb36c4bfc4e5c179333e436c8940b612303ba/compose/snippets/src/main/java/com/example/compose/snippets/layouts/ConstraintLayoutSnippets.kt
    @Composable
    fun ConstraintLayoutWithGuidelines() {
        ConstraintLayout(
            modifier = Modifier.fillMaxWidth().height(500.dp)
        ) {
            val (text) = createRefs()
            val guideline = createGuidelineFromStart(0.1f) // 50% from the start
            Text(
                text = "Centered Text",
                modifier = Modifier.constrainAs(text) {
                    start.linkTo(guideline)
                    top.linkTo(parent.top, margin = 16.dp)
                }
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("FirstFragment","MainActivity  onCreate");
        setContent {
            MaterialTheme {
                Surface {
                    Column( modifier = Modifier
                        .fillMaxSize()
//                        .verticalScroll(rememberScrollState())
                        .padding(8.dp),
                        verticalArrangement = Arrangement.spacedBy(8.dp)) {
                        ConstraintLayoutWithGuidelines()
//                        SlideInFromLeftDemo()
                        GraphicsLayer01()
                        GraphicsLayer02()
                        RepeatingRotationAnimation()
                        AnimateSizeAsState01()
//                        Spring()
//                        Slide()
//                        Scale()
//                        HorizontalPager01()
//                        FlowRowSimpleUsageExample()
//                        BasicAnimatedVisibilityDemo()
//                        AnimatedContentSizeDemo()
//                        SimpleAnimatedContentSizeDemo()
//                        AnimatedVisibility01()
//                        Offset01()
//                        BackgroundAlphaAnim01()
//                        TextColorAnim01()
//                        AnimatedVisibility01()
//                        BoxWithBackgroundColor()
                    }
                }
            }
        }
    }

    companion object{
    }
}